Writing Health Correlation Data PRO
In the Scripting app, you can write correlated health data to Apple HealthKit using the global HealthCorrelation.create() method and Health.saveCorrelation(). A correlation represents a relationship between multiple health samples, such as a blood pressure reading that includes both systolic and diastolic values, or a meal record that includes nutritional quantities.
This guide explains how to create and save a correlation sample.
What Is a Correlation?
A correlation groups related health data samples into a single, logical record. HealthKit currently supports the following correlation types:
"bloodPressure"— includes two quantity samples:"bloodPressureSystolic"and"bloodPressureDiastolic""food"— can include multiple nutritional quantity samples such as calories, protein, carbohydrates, etc.
1. Create Related Quantity Samples
Before creating a correlation, you must first create the individual HealthQuantitySample instances that will be included.
Example: Blood Pressure Samples
Check that both samples are not null before proceeding.
2. Create the Correlation
Use HealthCorrelation.create() to group the samples into a correlation.
Parameters
type:"bloodPressure"or"food"startDate: Start of the eventendDate: End of the eventobjects: An array ofHealthQuantitySample(orHealthCategorySample) instances to includemetadata(optional): Additional metadata (e.g., source, context)
Example
3. Save the Correlation to HealthKit
Use Health.saveCorrelation() to persist the correlation data to the HealthKit store.
Full Example: Writing a Blood Pressure Correlation
Notes
-
All quantity samples in the correlation must have matching or consistent time ranges.
-
For
"bloodPressure", the correlation must include bothsystolicanddiastolicsamples. -
For
"food", you may include multiple samples like:"dietaryEnergyConsumed"→HealthUnit.kilocalorie()"dietaryProtein"→HealthUnit.gram()"dietaryCarbohydrates"→HealthUnit.gram()
-
Correlation creation returns
nullif invalid or incomplete.
